package cetcbigdata.da.service.hainan;

import cetcbigdata.da.base.Base;
import cetcbigdata.da.service.hainan.util.AllInfo;
import com.alibaba.datax.common.plugin.RecordSender;
import com.alibaba.fastjson.JSONObject;

import java.util.List;

/**
 * 政务公开
 * 掌上办服务清单
 */

public class ZhangShangBanFuWuList extends Base {
    @Override
    public void getData(RecordSender recordSender, List<Object> fieldsList) {

    }

    @Override
    public JSONObject getDataDemo() {
        try {
            return this.parseListPage();
        }catch (Exception e){
            e.printStackTrace();
        }
        return null;
    }

    private JSONObject parseListPage() {
        //创建 详情页信息对象
        AllInfo allInfo = new AllInfo();
        //当前页
        int currentPage = 1;
        //总页数
        int pages = 0;
        while (pages != currentPage){
            String listUrl = "https://wssp.hainan.gov.cn/wssp3/heatTheme/getMatterByIdAndCode";
            JSONObject listHeader = new JSONObject();
            listHeader.put("Referer", "https://wssp.hainan.gov.cn/hnwt/serviceList-palm?");
            listHeader.put("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko)");
            JSONObject listParmas = new JSONObject();
            listParmas.put("deptid","");
            listParmas.put("topicid","3dc563324c064e0ab7bca68736ef1077");
            listParmas.put("xzqhid","ALL");
            listParmas.put("pageNum",currentPage);
            listParmas.put("pageSize","10");
            listParmas.put("tasktype","");
            listParmas.put("taskname","");
            listParmas.put("issub","1");

            String listStrResponse = postFormReq(listUrl,listHeader,listParmas);
            JSONObject listResponse = JSONObject.parseObject(listStrResponse);
            for (Object o : listResponse.getJSONObject("data").getJSONArray("resultList")){
                JSONObject reqData = (JSONObject) o;
                String id = reqData.getString("id");
                String sourcekey = reqData.getString("sourcekey");
                if (null != sourcekey && sourcekey.length()>0) {
                    allInfo.setSourcekey(reqData.getString("sourcekey"));
                    allInfo.setId(reqData.getString("id"));
                    allInfo.init();
                } else {
                    allInfo.setSourcekey(reqData.getString("sourcekey"));
                    allInfo.setId(reqData.getString("id"));
                    allInfo.init();
                    JSONObject params = new JSONObject();
                    params.put("id", id);
                    allInfo.setAllInfoParmas(params);
                }
                JSONObject result;
                try {
                    result = allInfo.getAllInfo();
                } catch (Exception e) {
                    break;
                }
                System.out.println(currentPage+"\n"+result.getString("名称")+"\n"+result);//测试用代码
                return processResult(
                        null,
                        null,
                        result.getString("地址"),
                        allInfo.getResponce(),
                        result,
                        "海南省",
                        "政务公开-掌上办服务清单"
                );
            }
            //更新当前页和总页数
            currentPage++;
            pages = listResponse.getJSONObject("data").getJSONObject("pageMap").getInteger("pages") + 1;
        }
        return null;
    }

    public static void main(String[] args) {
        ZhangShangBanFuWuList zhangShangBanFuWuList = new ZhangShangBanFuWuList();
        zhangShangBanFuWuList.getDataDemo();
    }
}
